home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0070_Multiple instance prevention.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  1.2 KB  |  59 lines

  1.  
  2. Following on from " "Sean Gates" <sgates@goofy.iafrica.com>", place the
  3. following at the start of your name.dpr:
  4.  
  5. begin
  6.   if HPrevInst <> 0 then begin
  7.     ActivatePreviousInstance;
  8.     Exit;
  9.   end;
  10.   .
  11.   .
  12.   .
  13.  
  14. and include the following unit:
  15.  
  16. -------------- cut here -------------------- }
  17. unit PrevInst;
  18.  
  19. interface
  20.  
  21. uses WinProcs, WinTypes, SysUtils;
  22.  
  23. type
  24.   PHWnd = ^HWnd;
  25.  
  26. function EnumFunc(Wnd : HWnd; TargetWindow : PHWnd): Bool; export;
  27. procedure ActivatePreviousInstance;
  28.  
  29. implementation
  30.  
  31. function EnumFunc(Wnd : HWnd; TargetWindow : PHWnd): Bool;
  32. var
  33.   ClassName : array [0..30] of char;
  34. begin
  35.   Result := True;
  36.   if GetWindowWord(Wnd,GWW_HINSTANCE) = HPrevInst then begin
  37.     GetClassName(Wnd,ClassName,30);
  38.     if StrIComp(ClassName,'TApplication') = 0 then begin
  39.       TargetWindow^ := Wnd;
  40.       Result := False;
  41.     end;
  42.   end;
  43. end;
  44.  
  45. procedure ActivatePreviousInstance;
  46. var
  47.   PrevInstWnd : HWnd;
  48. begin
  49.   PrevInstWnd := 0;
  50.   EnumWindows(@EnumFunc,Longint(@PrevInstWnd));
  51.   if PrevInstWnd <> 0 then
  52.     if IsIconic(PrevInstWnd) then
  53.       ShowWindow(PrevInstWnd,SW_RESTORE)
  54.     else
  55.       BringWindowToTop(PrevInstWnd);
  56. end;
  57.  
  58. end.
  59.